package com.ruoyi.web.controller.common;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

import java.util.List;

import javax.annotation.Resource;

import lombok.extern.slf4j.Slf4j;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.Page;
import com.ruoyi.system.domain.SmsHomeAdvertise;
import com.ruoyi.system.service.ISmsHomeAdvertiseService;

/**
 * <p>
 * 首页轮播广告表
 * </p>
 *
 * @author flowr
 * @since 2019-04-19
 */
@Slf4j
@RestController
@Api(tags = "SmsHomeAdvertiseController", description = "首页轮播广告表管理")
@RequestMapping("/sms/SmsHomeAdvertise")
public class SmsHomeAdvertiseController {
    @Resource
    private ISmsHomeAdvertiseService ISmsHomeAdvertiseService;
    //@Resource
    //private RedisService redisService;

    //@SysLog(MODULE = "sms", REMARK = "根据条件查询所有首页轮播广告表列表")
    @ApiOperation("根据条件查询所有首页轮播广告表列表")
    @GetMapping(value = "/list")
    public Object getSmsHomeAdvertiseByPage(SmsHomeAdvertise entity,
                                            @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                            @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize
    ) {
        try {
            return ISmsHomeAdvertiseService.page(new Page<SmsHomeAdvertise>(pageNum, pageSize), new QueryWrapper<>(entity));
        } catch (Exception e) {
            log.error("根据条件查询所有首页轮播广告表列表：%s", e.getMessage(), e);
        }
        return null;
    }

    //@SysLog(MODULE = "sms", REMARK = "保存首页轮播广告表")
    @ApiOperation("保存首页轮播广告表")
    @PostMapping(value = "/create")
    public Object saveSmsHomeAdvertise(@RequestBody SmsHomeAdvertise entity) {
        try {
            entity.setClickCount(0);
            entity.setOrderCount(0);
            if (ISmsHomeAdvertiseService.save(entity)) {
                //保存时清空缓存
                //redisService.set(Rediskey.appletBannerKey + "2", "[]");
            	return null;
            }
        } catch (Exception e) {
            log.error("保存首页轮播广告表：%s", e.getMessage(), e);
            return null;
        }
        return null;
    }

    //@SysLog(MODULE = "sms", REMARK = "更新首页轮播广告表")
    @ApiOperation("更新首页轮播广告表")
    @PostMapping(value = "/update/{id}")
    public Object updateSmsHomeAdvertise(@RequestBody SmsHomeAdvertise entity) {
        try {
            if (ISmsHomeAdvertiseService.updateById(entity)) {
            	return null;
            }
        } catch (Exception e) {
            log.error("更新首页轮播广告表：%s", e.getMessage(), e);
            return null;
        }
        return null;
    }

    //@SysLog(MODULE = "sms", REMARK = "删除首页轮播广告表")
    @ApiOperation("删除首页轮播广告表")
    @GetMapping(value = "/delete/{id}")
    public Object deleteSmsHomeAdvertise(@ApiParam("首页轮播广告表id") @PathVariable Long id) {
        try {
           /* if (ValidatorUtils.empty(id)) {
                return new CommonResult().paramFailed("首页轮播广告表id");
            }*/
            if (ISmsHomeAdvertiseService.removeById(id)) {
            	return null;
            }
        } catch (Exception e) {
            log.error("删除首页轮播广告表：%s", e.getMessage(), e);
            return null;
        }
        return null;
    }

    //@SysLog(MODULE = "sms", REMARK = "给首页轮播广告表分配首页轮播广告表")
    @ApiOperation("查询首页轮播广告表明细")
    @GetMapping(value = "/{id}")
    public Object getSmsHomeAdvertiseById(@ApiParam("首页轮播广告表id") @PathVariable Long id) {
        try {
            /*if (ValidatorUtils.empty(id)) {
                return new CommonResult().paramFailed("首页轮播广告表id");
            }*/
            SmsHomeAdvertise coupon = ISmsHomeAdvertiseService.getById(id);
            return null;
        } catch (Exception e) {
            log.error("查询首页轮播广告表明细：%s", e.getMessage(), e);
            return null;
        }

    }

    @ApiOperation(value = "批量删除首页轮播广告表")
    @RequestMapping(value = "/delete/batch", method = RequestMethod.GET)
    @ResponseBody
    //@SysLog(MODULE = "pms", REMARK = "批量删除首页轮播广告表")
    public Object deleteBatch(@RequestParam("ids") List<Long> ids) {
        boolean count = ISmsHomeAdvertiseService.removeByIds(ids);
        if (count) {
        	return null;
        } else {
        	return null;
        }
    }

    //@SysLog(MODULE = "sms", REMARK = "修改上下线状态")
    @ApiOperation("修改上下线状态")
    @RequestMapping(value = "/update/status/{id}", method = RequestMethod.POST)
    @ResponseBody
    public Object updateStatus(@PathVariable Long id, Integer status) {
        SmsHomeAdvertise record = new SmsHomeAdvertise();
        record.setId(id);
        record.setStatus(status);
        return ISmsHomeAdvertiseService.updateById(record);

    }


    @ApiOperation("修改推荐排序")
    @RequestMapping(value = "/update/sort/{id}", method = RequestMethod.POST)
    @ResponseBody
    public Object updateSort(@PathVariable Long id, Integer sort) {
        int count = ISmsHomeAdvertiseService.updateSort(id, sort);
        if (count > 0) {
        	return null;
        }
        return null;
    }

    @ApiOperation("批量修改推荐状态")
    @RequestMapping(value = "/update/recommendStatus", method = RequestMethod.POST)
    @ResponseBody
    public Object updateRecommendStatus(@RequestParam("ids") List<Long> ids, @RequestParam Integer recommendStatus) {
        int count = ISmsHomeAdvertiseService.updateRecommendStatus(ids, recommendStatus);
        if (count > 0) {
        	return null;
        }
        return null;
    }
}
